<template>
    <div>
        <!-- 面包屑导航区域 -->
        <BreadCrumbs name1='订单管理' name2='订单列表'></BreadCrumbs>

        <!-- 卡片视图区域 -->
        <el-card>
            <el-row>
                <el-col :span="8">
                    <!-- 利用keyup.native 实时搜索 -->
                    <el-input placeholder="请输入内容" class="input-with-select" clearable @clear="getOrdersDate"
                        @keyup.native="getOrdersDate" @keydown.native.esc="clearandsearch" v-model="queryInfo.query">
                        <el-button slot="append" icon="el-icon-search" @click="getOrdersDate"></el-button>
                    </el-input>

                </el-col>
            </el-row>
            <el-row>
                <!-- 表格区域 -->
                <el-table :data="ordersList" stripe border style="width: 100%">
                    <el-table-column type="index" label="#"></el-table-column>
                    <el-table-column prop="order_number" label="订单编号" width="420px"></el-table-column>
                    <el-table-column prop="order_price" label="订单价格(元)"></el-table-column>
                    <el-table-column prop="pay_status" label="是否支付">
                        <template v-slot="scope">
                            <el-tag v-if="scope.row.pay_status" type="danger">未付款</el-tag>
                            <el-tag v-else type="success">已付款</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="is_send" label="是否发货"></el-table-column>
                    <el-table-column prop="update_time" label="下单时间">
                        <template v-slot="scope">
                            {{ scope.row.update_time | dateFormat }}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120px">
                        <template>
                            <el-tooltip class="item" effect="dark" content="修改地址" placement="top">
                                <el-button type="primary" icon="el-icon-edit" @click="edit" circle>
                                </el-button>
                            </el-tooltip>

                            <el-tooltip class="item" effect="dark" content="物流状态" placement="top">
                                <el-button type="success" icon="el-icon-map-location" @click="view" circle>
                                </el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>

                </el-table>
                <!-- 分页区域 -->
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :page-sizes="[7, 14, 25, 35]" :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </el-row>
        </el-card>

        <!-- 修改地址对话框 -->
        <el-dialog title="修改地址" :visible.sync="orderdialogVisible" width="50%" :before-close="handleClose">
            <el-form :model="numberValidateForm" :rules="addFormRules" ref="ValidateForm" label-width="100px"
                class="demo-ruleForm" :close-on-click-modal='true'>
                <el-form-item label="省市区/县" prop="address1">
                    <el-cascader v-model="numberValidateForm.address1" :options="cityData"
                        :props="{ expandTrigger: 'hover' }"></el-cascader>
                </el-form-item>

                <el-form-item label="商品名称" prop="address2">
                    <el-input v-model="numberValidateForm.address2"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="orderdialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="orderdialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 物流对话框 -->
        <el-dialog :title="title" :visible.sync="orderLogisticsDialogVisible" width="50%">

            <el-timeline>
                <el-timeline-item v-for="(activity, index) in activities" :key="index" :timestamp="activity.time">
                    {{ activity.context }}
                </el-timeline-item>
            </el-timeline>
        </el-dialog>

    </div>
</template>

<script>
import BreadCrumbs from '@/components/BreadCrumbs'
import { orderRules } from '@/mixins/rules.js'
import cityData from '@/components/order/citydata'
export default {
    mixins: [orderRules],
    data() {
        return {
            // 订单表格的数据
            ordersList: [],
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 7,
            },
            total: 0,

            // 修改对话框
            orderdialogVisible: false,
            numberValidateForm: {
                address1: [],
                address2: ''
            },
            // 查看物流对话框
            orderLogisticsDialogVisible: false,
            activities: [],
            title:'',
            // addFormRules: {
            //     order_addres: [{ required: true, message: '11', trigger: 'blur' }]
            // },
            // 存储名字和地址名字一样可以简写
            // cityData : cityData
            cityData
        }
    },
    methods: {
        async getOrdersDate() {
            const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
            if (res.meta.status !== 200) {
                this.$message.error(res.meta.msg)
            }
            this.ordersList = res.data.goods
            this.total = res.data.total
            // console.log(res);
        },
        clearandsearch() {
            // 按下esc清空搜索并请求
            this.queryInfo.query = ''
            this.getOrdersDate()
        },

        // 分页器部分
        handleSizeChange(pagesize) {
            //每页多少条
            this.queryInfo.pagesize = pagesize
            this.getOrdersDate()

        },
        handleCurrentChange(pagenum) {
            //当前页
            this.queryInfo.pagenum = pagenum
            this.getOrdersDate()
        },

        // 对话框
        handleClose() {
            this.orderdialogVisible = false

            this.$refs.ValidateForm.resetFields()

        },
        // 操作按钮
        edit() {
            this.orderdialogVisible = true
        },
        async view() {
            this.orderLogisticsDialogVisible = true
            const { data: res } = await this.$http.get('/kuaidi/1106975712662')
            if (res.meta.status !== 200) {
                this.$message.error(res.meta.msg)
            }
            // 接口失效。使用静态数据
            if (res.data[0].context == "查无结果") {
                this.title = '物流地址(查无结果，此表静态)'
                this.activities = [
                    {
                        "time": "2018-05-10 09:39:00",
                        "ftime": "2018-05-10 09:39:00",
                        "context": "已签收,感谢使用顺丰,期待再次为您服务",

                        "location": ""
                    },
                    {
                        "time": "2018-05-10 08:23:00",
                        "ftime": "2018-05-10 08:23:00",
                        "context": "[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件",
                        "location": ""
                    },
                    {
                        "time": "2018-05-10 07:32:00",
                        "ftime": "2018-05-10 07:32:00",
                        "context": "快件到达 [北京海淀育新小区营业点]",
                        "location": ""
                    },
                    {
                        "time": "2018-05-10 02:03:00",
                        "ftime": "2018-05-10 02:03:00",
                        "context": "快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]",
                        "location": ""
                    },
                    {
                        "time": "2018-05-09 23:05:00",
                        "ftime": "2018-05-09 23:05:00",
                        "context": "快件到达 [北京顺义集散中心]",
                        "location": ""
                    },
                    {
                        "time": "2018-05-09 21:21:00",
                        "ftime": "2018-05-09 21:21:00",
                        "context": "快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]",
                        "location": ""
                    },
                    {
                        "time": "2018-05-09 13:07:00",
                        "ftime": "2018-05-09 13:07:00",
                        "context": "顺丰速运 已收取快件",
                        "location": ""
                    },
                    {
                        "time": "2018-05-09 12:25:03",
                        "ftime": "2018-05-09 12:25:03",
                        "context": "卖家发货",
                        "location": ""
                    },
                    {
                        "time": "2018-05-09 12:22:24",
                        "ftime": "2018-05-09 12:22:24",
                        "context": "您的订单将由HLA（北京海淀区清河中街店）门店安排发货。",
                        "location": ""
                    },
                    {
                        "time": "2018-05-08 21:36:04",
                        "ftime": "2018-05-08 21:36:04",
                        "context": "商品已经下单",
                        "location": ""
                    }
                ]
            }else{
                this.title = '物流地址'
                this.activities  = res.data
            }
            console.log(res);

        },
    },
    created() {
        this.getOrdersDate()
    },
    components: {
        BreadCrumbs
    }
}
</script>

<style lang="less" scoped>
.el-table {
    margin: 15px 0;
}

.el-cascader {
    width: 100%;
}
</style>